﻿using de.wolfmarco.pas.Accounting.Database;
using de.wolfmarco.pas.Accounting.MVVM;
using de.wolfmarco.pas.Shared.Interfaces;
using de.wolfmarco.pas.Shared.MVVM;
using de.wolfmarco.pas.Shared.Regions;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using System;
using System.Linq;


namespace de.wolfmarco.pas.Accounting
{
    [Module(OnDemand = true, ModuleName = AccountingHelper.ModuleName)]
    public class AccountingModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public AccountingModule(IUnityContainer container, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
        }

        public void Initialize()
        {
            moduleManager.LoadModuleCompleted += moduleManager_LoadModuleCompleted;

            new AccountingDatabase();
        }

        private void moduleManager_LoadModuleCompleted( object sender, LoadModuleCompletedEventArgs e )
        {
            if (e.ModuleInfo.ModuleName == AccountingHelper.ModuleName)
            {
                registerModuleView();
                registerScheduledCashFlowView();
            }
        }

        private void registerModuleView()
        {
            TransientLifetimeManager transientLifetimeManager = new TransientLifetimeManager();

            NavigateViewModel viewModel = new NavigateViewModel(regionManager, moduleManager);
            //container.RegisterInstance(typeof(NavigateViewModel), "AccountingNavigateViewModel", viewModel);

            InjectionMember[] injectionMember = new InjectionMember[]
            {
                new InjectionProperty( "DataContext", viewModel )
            };

            container.RegisterType(typeof(object), typeof(AccountingModuleView), AccountingHelper.ModuleView, transientLifetimeManager, injectionMember);
            regionManager.RegisterViewWithRegion(RegionNames.ModuleRegion, () => container.Resolve<AccountingModuleView>(AccountingHelper.ModuleView));


            ModuleViewModel moduleViewModel = container.Resolve<ModuleViewModel>("StartMenu");
            moduleViewModel.ModuleRegionChanged += viewModel_ModuleRegionChanged;
        }

        private void registerScheduledCashFlowView()
        {
            TransientLifetimeManager transientLifetimeManager = new TransientLifetimeManager();

            ScheduledCashFlowViewModel viewModel = new ScheduledCashFlowViewModel(regionManager, moduleManager);

            InjectionMember[] injectionMember = new InjectionMember[]
            {
                new InjectionProperty( "DataContext", viewModel )
            };

            container.RegisterType(typeof(object), typeof(ScheduledCashFlowView), typeof(ScheduledCashFlowView).Name, transientLifetimeManager, injectionMember);
            regionManager.RegisterViewWithRegion(RegionNames.ModuleRegion, () => container.Resolve<ScheduledCashFlowView>(typeof(ScheduledCashFlowView).Name));
        }

        private void viewModel_ModuleRegionChanged(object sender, RibbonTabEventArgs e)
        {
            AccountingRibbonTabView ribbonTabView;
            if ( container.IsRegistered<AccountingRibbonTabView>() )
            {
                ribbonTabView = container.Resolve<AccountingRibbonTabView>();
            }
            else
            {
                ribbonTabView = new AccountingRibbonTabView();
                ribbonTabView.DataContext = new AccountingRibbonTabViewModel(regionManager, moduleManager);
                container.RegisterInstance<AccountingRibbonTabView>(ribbonTabView);
            }
            e.AddRibbonTab(ribbonTabView);
        }
    }
}
